package com.cskaoyan.wordmemorize.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cskaoyan.wordmemorize.common.aspect.operate.OperateLog;
import com.cskaoyan.wordmemorize.common.constant.ResultCodeEnum;
import com.cskaoyan.wordmemorize.common.exception.BusinessException;
import com.cskaoyan.wordmemorize.converter.VocConverter;
import com.cskaoyan.wordmemorize.dao.entity.*;
import com.cskaoyan.wordmemorize.dao.mapper.*;
import com.cskaoyan.wordmemorize.dto.admin.*;
import com.cskaoyan.wordmemorize.dto.app.DisplayBookVocDTO;
import com.cskaoyan.wordmemorize.dto.app.DisplayChapterDTO;
import com.cskaoyan.wordmemorize.dto.app.UserBookStatisticsDTO;
import com.cskaoyan.wordmemorize.request.*;
import com.cskaoyan.wordmemorize.service.FileUploadService;
import com.cskaoyan.wordmemorize.service.VocService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class VocServiceImpl implements VocService {


    @Autowired
    VocMapper vocMapper;

    @Autowired
    VocConverter vocConverter;


    @Autowired
    VocMeaningMapper vocMeaningMapper;

    @Autowired
    ExampleSentenceMapper exampleSentenceMapper;

    @Autowired
    VocChapBookMapper vocChapBookMapper;

    @Autowired
    VocChapterMapper vocChapterMapper;

    @Autowired
    VocCategoryMapper vocCategoryMapper;


    @Autowired
    VocBookMapper vocBookMapper;

    @Autowired
    FileUploadService fileUploadService;

    /**
     * 分页查询单词信息(物料管理-词汇管理)
     *
     * @param pageQuery
     * @return 包含单词信息的分页数据
     */
    @Override
    public PageDTO<VocDTO> getVocabularies(VocPageRequest pageQuery) {
        List<Long> vocIdList = null;
        // 需要先查关系表，获取vocId列表
        // 请求中可能会包含书籍ID或章节ID，需要从关系表中筛选符合条件的单词ID
        if ((pageQuery.getBookId() != null && !pageQuery.getBookId().trim().isEmpty()) ||
                (pageQuery.getChapterId() != null && !pageQuery.getChapterId().trim().isEmpty())) {
            // 查询单词与章节、书籍的关系
            LambdaQueryWrapper<VocChapBookDO> relWrapper = new LambdaQueryWrapper<>();
            // 只查询未被删除的关系
            relWrapper.eq(VocChapBookDO::getIsDeleted, 0);
            if (pageQuery.getBookId() != null && !pageQuery.getBookId().trim().isEmpty()) {
                // 请求中包含书籍ID，添加书籍ID的查询条件
                relWrapper.eq(VocChapBookDO::getBookId, pageQuery.getBookId());
            }
            if (pageQuery.getChapterId() != null && !pageQuery.getChapterId().trim().isEmpty()) {
                // 请求中包含章节ID，添加章节ID的查询条件
                relWrapper.eq(VocChapBookDO::getChapterId, pageQuery.getChapterId());
            }
            List<VocChapBookDO> rels = vocChapBookMapper.selectList(relWrapper);

            vocIdList = new ArrayList<>();
            // 遍历关系记录列表，将其中的单词ID添加到单词ID列表中
            for (VocChapBookDO rel : rels) {
                vocIdList.add(rel.getVocId());
            }
            if (vocIdList.isEmpty()) {
                // 若单词ID列表为空，说明没有符合条件的单词，直接返回空的分页数据
                return vocConverter.vocList2Page(new ArrayList<>(), 0L);
            }
        }
        // 查询单词表
        LambdaQueryWrapper<VocDO> wrapper = new LambdaQueryWrapper<>();
        // 只查询未被删除的单词
        wrapper.eq(VocDO::getIsDeleted, 0);

        if (vocIdList != null) {
            // 单词ID列表不为空，(查询的单词需要在单词列表中 in方法)
            wrapper.in(VocDO::getId, vocIdList);
        }
        if (pageQuery.getKeyword() != null && !pageQuery.getKeyword().trim().isEmpty()) {
            // 请求中包含关键词，添加单词内容的模糊查询
            wrapper.like(VocDO::getContent, pageQuery.getKeyword());
        }
        // 单词一般按照a-b-c这样排序,所以用字典序排序(即直接用单词本身来排序)
        wrapper.orderByAsc(VocDO::getContent);

        List<VocDO> vocList;  // 单词列表

        long total;  // 总条数

        if (pageQuery.getPageNum() == null || pageQuery.getCount() == null) {
            // 分页参数为空，查询所有
            vocList = vocMapper.selectList(wrapper);
            total = vocList.size();
        } else {
            Page<VocDO> page = new Page<>(pageQuery.getPageNum(), pageQuery.getCount());
            Page<VocDO> result = vocMapper.selectPage(page, wrapper);
            vocList = result.getRecords();
            total = result.getTotal();
        }
        // 释义和例句
        for (VocDO voc : vocList) {
            // 释义
            List<VocMeaningDO> meanings = vocMeaningMapper.selectList(
                    new LambdaQueryWrapper<VocMeaningDO>().eq(VocMeaningDO::getVocId, voc.getId()));
            voc.setInterpretations(meanings);
            // 例句
            List<ExampleSentenceDO> examples = exampleSentenceMapper.selectList(
                    new LambdaQueryWrapper<ExampleSentenceDO>().eq(ExampleSentenceDO::getVocId, voc.getId()));
            voc.setExamples(examples);
        }
        return vocConverter.vocList2Page(vocList, total);
    }



    /**
     * 添加单词
     * @param command
     */
    @Override
    @OperateLog(dataType = "单词", operateType = 1, description = "新增单词")
    public void addVoc(VocCommand command) {

        // 判断要添加的单词是否存在
        LambdaQueryWrapper<VocDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(VocDO::getContent, command.getContent());

        VocDO vocDO = vocMapper.selectOne(queryWrapper);
        if (vocDO != null) {
           throw new BusinessException(ResultCodeEnum.VOC_EXISTED);
        }

        // 保存单词
        VocDO voc = vocConverter.command2VocDO(command);
        // 保存基本的单词数据
        vocMapper.insert(voc);


        // 保存单词释义
        List<VocMeaningCommand> interpretations = command.getInterpretations();
        List<VocMeaningDO> vocMeaningDOs = vocConverter.commands2VocMeaningDOs(interpretations);

        if (!CollectionUtils.isEmpty(vocMeaningDOs)) {

            vocMeaningDOs.forEach(vocMeaningDO -> {

                // 设置释义多对应的单词id
                vocMeaningDO.setVocId(voc.getId());
                vocMeaningMapper.insert(vocMeaningDO);
            });
        }

        // 保存例句
        List<ExampleSentenceCommand> examples = command.getExamples();
        List<ExampleSentenceDO> exampleSentenceDOs = vocConverter.commands2ExampleSentenceDOs(examples);
        if (!CollectionUtils.isEmpty(exampleSentenceDOs)) {
            exampleSentenceDOs.forEach(exampleSentenceDO -> {

                // 设置例句对应的单词id
                exampleSentenceDO.setVocId(voc.getId());
                exampleSentenceMapper.insert(exampleSentenceDO);
            });
        }


        // 保存单词-章节-书籍关系
        List<VocChapBookCommand> relations = command.getRelations();
        List<VocChapBookDO> vocChapBookDOs = vocConverter.commands2VocChapBookDOs(relations);
        if (!CollectionUtils.isEmpty(vocChapBookDOs)) {
            vocChapBookDOs.forEach(vocChapBookDO -> {

                // 设置单词id
                vocChapBookDO.setVocId(voc.getId());
                vocChapBookMapper.insert(vocChapBookDO);


                //修改章节中包含的单词数量
                if (vocChapBookDO.getChapterId() != null) {

                    LambdaUpdateWrapper<VocChapterDO> chapterWrapper = new LambdaUpdateWrapper<>();
                    chapterWrapper.eq(VocChapterDO::getId, vocChapBookDO.getChapterId())
                            .setSql("voc_count = voc_count + 1");
                    vocChapterMapper.update(null, chapterWrapper);

                }

                // 书籍中包含的单词数量
                LambdaUpdateWrapper<VocBookDO> vocBookWrapper = new LambdaUpdateWrapper<>();
                vocBookWrapper.eq(VocBookDO::getId, vocChapBookDO.getBookId())
                        .setSql("voc_count = voc_count + 1");

                vocBookMapper.update(null, vocBookWrapper);
            });
        }

    }

    /**
     *  修改单词
     * @param command
     */
    @Override
    @Transactional
    @OperateLog(dataType = "单词", operateType = 4, description = "修改单词")
    public void updateVoc(VocCommand command) {
        // 检查单词是否在数据库中存在,只有单词存在才能修改
        LambdaQueryWrapper<VocDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(VocDO::getId, command.getId());

        VocDO vocDO = vocMapper.selectOne(queryWrapper);
        if (vocDO == null) {
            throw new BusinessException(ResultCodeEnum.VOC_NOT_EXISTED);
        }

        // 更新单词基本信息
        VocDO voc = vocConverter.command2VocDO(command);
        vocMapper.updateById(voc);

        // 处理释义
        List<VocMeaningCommand> interpretations = command.getInterpretations();
        if (!CollectionUtils.isEmpty(interpretations)) {
            for (VocMeaningCommand meaning : interpretations) {
                VocMeaningDO vocMeaningDO = vocConverter.command2VocMeaningDO(meaning);
                vocMeaningDO.setVocId(voc.getId());

                if (meaning.getId() == null) {
                    // 若释义 ID 为空，说明是新增释义，执行插入操作
                    vocMeaningMapper.insert(vocMeaningDO);
                } else {
                    // 若释义 ID 不为空，说明是更新释义，执行更新操作
                    vocMeaningMapper.updateById(vocMeaningDO);
                }
            }
        }

        // 处理例句
        List<ExampleSentenceCommand> examples = command.getExamples();
        if (!CollectionUtils.isEmpty(examples)) {
            for (ExampleSentenceCommand example : examples) {
                ExampleSentenceDO exampleSentenceDO = vocConverter.command2ExampleSentenceDO(example);
                exampleSentenceDO.setVocId(voc.getId());
                if (example.getId() == null) {
                    // 若例句 ID 为空，说明是新增例句，执行插入操作
                    exampleSentenceMapper.insert(exampleSentenceDO);
                } else {
                    // 若例句 ID 不为空，说明是更新例句，执行更新操作
                    exampleSentenceMapper.updateById(exampleSentenceDO);
                }
            }
        }

        // 处理单词-章节-书籍关系
        // 查询旧关系
        List<VocChapBookDO> oldRelations = vocChapBookMapper.selectList(
                new LambdaQueryWrapper<VocChapBookDO>().eq(VocChapBookDO::getVocId, voc.getId()));
        // 删除旧关系并减数量
        if (!CollectionUtils.isEmpty(oldRelations)) {
            for (VocChapBookDO old : oldRelations) {
                vocChapBookMapper.deleteById(old.getId());
                // 章节数量减1
                if (old.getChapterId() != null) {
                    vocChapterMapper.update(null, new LambdaUpdateWrapper<VocChapterDO>()
                            .eq(VocChapterDO::getId, old.getChapterId())
                            .setSql("voc_count = voc_count - 1"));
                }
                // 书籍数量减1
                vocBookMapper.update(null, new LambdaUpdateWrapper<VocBookDO>()
                        .eq(VocBookDO::getId, old.getBookId())
                        .setSql("voc_count = voc_count - 1"));
            }
        }
        // 插入新关系并加数量
        List<VocChapBookCommand> relations = command.getRelations();
        List<VocChapBookDO> vocChapBookDOs = vocConverter.commands2VocChapBookDOs(relations);
        if (!CollectionUtils.isEmpty(vocChapBookDOs)) {
            for (VocChapBookDO vocChapBookDO : vocChapBookDOs) {
                vocChapBookDO.setVocId(voc.getId());
                // 判断关系是否存在（不管isDeleted）
                LambdaQueryWrapper<VocChapBookDO> relWrapper = new LambdaQueryWrapper<>();
                relWrapper.eq(VocChapBookDO::getBookId, vocChapBookDO.getBookId())
                        .eq(VocChapBookDO::getChapterId, vocChapBookDO.getChapterId())
                        .eq(VocChapBookDO::getVocId, vocChapBookDO.getVocId());
                VocChapBookDO exist = vocChapBookMapper.selectOne(relWrapper);
                if (exist != null) {
                    if (exist.getIsDeleted() != null && exist.getIsDeleted() == 1) {
                        // 逻辑删除，恢复
                        exist.setIsDeleted(0);

                        vocChapBookMapper.updateById(exist);
                        // 数量加1
                        if (vocChapBookDO.getChapterId() != null) {
                            vocChapterMapper.update(null, new LambdaUpdateWrapper<VocChapterDO>()
                                    .eq(VocChapterDO::getId, vocChapBookDO.getChapterId())
                                    .setSql("voc_count = voc_count + 1"));
                        }
                        vocBookMapper.update(null, new LambdaUpdateWrapper<VocBookDO>()
                                .eq(VocBookDO::getId, vocChapBookDO.getBookId())
                                .setSql("voc_count = voc_count + 1"));
                    }
                    // 已存在且未删除，不做插入
                    continue;
                } else {
                    // 不存在，插入新关系
                    vocChapBookMapper.insert(vocChapBookDO);
                    if (vocChapBookDO.getChapterId() != null) {
                        vocChapterMapper.update(null, new LambdaUpdateWrapper<VocChapterDO>()
                                .eq(VocChapterDO::getId, vocChapBookDO.getChapterId())
                                .setSql("voc_count = voc_count + 1"));
                    }
                    vocBookMapper.update(null, new LambdaUpdateWrapper<VocBookDO>()
                            .eq(VocBookDO::getId, vocChapBookDO.getBookId())
                            .setSql("voc_count = voc_count + 1"));
                }
            }
        }
    }

    /**
     * 删除指定单词
     * @param vocId
     * @return
     */
    @Override
    @Transactional
    @OperateLog(dataType = "单词", operateType = 2, description = "删除单词", logReturnValue = true)
    public DeleteVocOperateDTO deleteVocabulary(Long vocId) {
        VocDO voc = vocMapper.selectById(vocId);
        if (voc == null) {
            return null;
        }
        // 删除与书籍-章节的所有关系，维护数量
        List<VocChapBookDO> relations = vocChapBookMapper.selectList(
                new LambdaQueryWrapper<VocChapBookDO>().eq(VocChapBookDO::getVocId, vocId));
        if (!CollectionUtils.isEmpty(relations)) {
            for (VocChapBookDO relation : relations) {
                vocChapBookMapper.deleteById(relation.getId());
                if (relation.getChapterId() != null) {
                    vocChapterMapper.update(null, new LambdaUpdateWrapper<VocChapterDO>()
                            .eq(VocChapterDO::getId, relation.getChapterId())
                            .setSql("voc_count = voc_count - 1"));
                }
                vocBookMapper.update(null, new LambdaUpdateWrapper<VocBookDO>()
                        .eq(VocBookDO::getId, relation.getBookId())
                        .setSql("voc_count = voc_count - 1"));
            }
        }
        // 删除释义
        vocMeaningMapper.delete(new LambdaQueryWrapper<VocMeaningDO>().eq(VocMeaningDO::getVocId, vocId));
        // 删除例句
        exampleSentenceMapper
                .delete(new LambdaQueryWrapper<ExampleSentenceDO>().eq(ExampleSentenceDO::getVocId, vocId));
        // 删除单词本身
        vocMapper.deleteById(vocId);
        // 构造删除的DTO对象
        DeleteVocOperateDTO dto = new DeleteVocOperateDTO();
        dto.setId(vocId);
        dto.setContent(voc.getContent());
        return dto;
    }

    /**
     * 批量删除单词
     *
     * @param command
     * @return
     */
    @Override
    @Transactional
    @OperateLog(dataType = "单词", operateType = 3, description = "批量删除单词", logReturnValue = true)
    public List<DeleteVocOperateDTO> deleteVocabularies(BatchDeleteCommand command) {
        List<String> ids = command.getIds();
        if (CollectionUtils.isEmpty(ids)) {
            return null;
        }

        List<DeleteVocOperateDTO> result = new ArrayList<>();
        for (String idStr : ids) {
            Long vocId;
            try {
                vocId = Long.valueOf(idStr);
            } catch (NumberFormatException e) {
                continue;
            }
            DeleteVocOperateDTO dto = deleteVocabulary(vocId);
            if (dto != null) {
                result.add(dto);
            }
        }
        return result;
    }

    /**
     * 获取单词与章节-书籍的关系
     * @param vocId
     * @return
     */
    @Override
    public List<VocChapBookDTO> getVocRelations(Long vocId) {
        // 查询所有关系
        List<VocChapBookDO> relations = vocChapBookMapper.selectList(
                new LambdaQueryWrapper<VocChapBookDO>().eq(VocChapBookDO::getVocId, vocId));
        if (CollectionUtils.isEmpty(relations)) {
            return new ArrayList<>();
        }
        // 补全DTO信息
        for (VocChapBookDO relation : relations) {
            // 章节名
            if (relation.getChapterId() != null) {
                VocChapterDO chapter = vocChapterMapper.selectById(relation.getChapterId());
                if (chapter != null) {
                    relation.setChapterName(chapter.getChapterName());
                    relation.setBookId(chapter.getBookId());
                    relation.setBookName(chapter.getBookName());
                }
            }
            // 书籍名
            if (relation.getBookId() != null && (relation.getBookName() == null || relation.getBookName().isEmpty())) {
                VocBookDO book = vocBookMapper.selectById(relation.getBookId());
                if (book != null) {
                    relation.setBookName(book.getName());
                    relation.setCategoryId(book.getBcId());
                }
            }
            // 类目名
            if (relation.getCategoryId() != null) {
                VocCategoryDO category = vocCategoryMapper.selectById(relation.getCategoryId());
                if (category != null) {
                    relation.setCategoryName(category.getCategoryName());
                }
            }
        }
        return vocConverter.vocChapBookDOs2DTOs(relations);
    }

    @Override
    public PageDTO<DisplayBookVocDTO> getDisplayBookVocsInfo(AppVocPageRequest pageRequest) {
        return null;
    }

    @Override
    public PageDTO<DisplayChapterDTO> getDisplayBookChapterVocsInfo(AppVocPageRequest pageRequest) {
        return null;
    }


    @Override
    public VocDTO getVocDetail(Long vocId, Long userId) {
        return null;
    }

    @Override
    public UserBookStatisticsDTO getUserChooseCountInBook(Long userId, Long bookId) {
        return null;
    }

    /**
     * 校验Excel文件
     * @param file
     * @throws IOException
     */
    @Override
    @Transactional(readOnly = true)
    public void vocExcelValidate(MultipartFile file) throws IOException {
        VocExcelListener listener = new VocExcelListener();
        EasyExcel.read(file.getInputStream(), VocExcelDTO.class, listener).sheet().doRead();

        // 校验数据
        List<VocExcelDTO> dataList = listener.getDataList();
        if (CollectionUtils.isEmpty(dataList)) {
            throw new BusinessException(ResultCodeEnum.VOC_EXCEL_PARSE_ERROR);
        }

        // 检查重复单词
        for (int i = 0; i < dataList.size(); i++) {
            VocExcelDTO dto = dataList.get(i);
            LambdaQueryWrapper<VocDO> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(VocDO::getContent, dto.getContent());
            if (vocMapper.selectCount(queryWrapper) > 0) {
                throw new BusinessException(ResultCodeEnum.VOC_EXISTED);
            }
        }
    }

    /**
     * 从Excel文件导入单词
     * @param file
     * @param userId
     * @return
     * @throws IOException
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    @OperateLog(dataType = "单词", operateType = 6, description = "Excel导入单词", logReturnValue = true)
    public ExcelAddOperateDTO addVocExcel(MultipartFile file, Long userId) throws IOException {
        VocExcelListener listener = new VocExcelListener();
        EasyExcel.read(file.getInputStream(), VocExcelDTO.class, listener).sheet().doRead();

        List<VocExcelDTO> dataList = listener.getDataList();
        int successCount = 0;

        for (VocExcelDTO dto : dataList) {
            try {
                saveVocFromExcel(dto);
                successCount++;
            } catch (Exception e) {
                // 记录错误日志
                e.printStackTrace();
            }
        }

        return new ExcelAddOperateDTO();
    }
    private void saveVocFromExcel(VocExcelDTO dto) {
        // 1. 检查单词是否已存在
        LambdaQueryWrapper<VocDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(VocDO::getContent, dto.getContent());
        if (vocMapper.selectCount(queryWrapper) > 0) {
            throw new BusinessException(ResultCodeEnum.VOC_EXISTED);
        }

        // 2. 保存单词基本信息
        VocDO vocDO = vocConverter.vocExcelDTO2VocDO(dto);
        vocMapper.insert(vocDO);
        Long vocId = vocDO.getId();

        // 3. 保存释义（使用转换器的命令转换方法）
        List<VocMeaningCommand> meaningCommands = parseMeaningCommands(dto.getInterpretations());
        List<VocMeaningDO> meanings = vocConverter.commands2VocMeaningDOs(meaningCommands);
        if (!CollectionUtils.isEmpty(meanings)) {
            for (VocMeaningDO meaning : meanings) {
                meaning.setVocId(vocId);
                vocMeaningMapper.insert(meaning);
            }
        }

        // 4. 保存例句（使用转换器的命令转换方法）
        List<ExampleSentenceCommand> exampleCommands = parseExampleCommands(dto.getExampleSentences());
        List<ExampleSentenceDO> examples = vocConverter.commands2ExampleSentenceDOs(exampleCommands);
        if (!CollectionUtils.isEmpty(examples)) {
            for (ExampleSentenceDO example : examples) {
                example.setVocId(vocId);
                exampleSentenceMapper.insert(example);
            }
        }

    }

    private List<VocMeaningCommand> parseMeaningCommands(String interpretations) {
        List<VocMeaningCommand> commands = new ArrayList<>();
        if (interpretations == null || interpretations.isEmpty()) {
            return commands;
        }

        // 解析释义（格式：词性. 释义内容）
        String[] lines = interpretations.split("\\r?\\n");
        for (String line : lines) {
            if (line.trim().isEmpty()) {
                continue;
            }

            VocMeaningCommand command = new VocMeaningCommand();
            String[] parts = line.split("\\.", 2);
            if (parts.length >= 2) {
                command.setType(parts[0].trim());
                command.setInterContent(parts[1].trim());
            } else {
                command.setInterContent(parts[0].trim());
            }
            commands.add(command);
        }
        return commands;
    }

    private List<ExampleSentenceCommand> parseExampleCommands(String exampleSentences) {
        List<ExampleSentenceCommand> commands = new ArrayList<>();
        if (exampleSentences == null || exampleSentences.isEmpty()) {
            return commands;
        }

        // 解析例句（格式：英文例句\n中文翻译）
        String[] lines = exampleSentences.split("\\r?\\n");
        for (int i = 0; i < lines.length - 1; i += 2) {
            ExampleSentenceCommand command = new ExampleSentenceCommand();
            command.setContent(lines[i].trim());
            command.setMeaning(lines[i + 1].trim());
            commands.add(command);
        }
        return commands;
    }

    /**
     * 删除单词音频文件
     * @param id
     * @param type
     * @param name
     * @return
     */
    @Override
    @OperateLog(dataType = "单词", operateType = 2, description = "删除单词音频文件", logReturnValue = true)
    public DeleteVocFileOperateDTO deleteFile(Long id, String type, String name) {
        // 1. 查询单词信息
        VocDO vocDO = vocMapper.selectById(id);
        if (vocDO == null) {
            throw new BusinessException(ResultCodeEnum.VOC_NOT_EXISTED);
        }

        // 2. 获取要删除的音频文件URL
        String fileUrl = null;
        if ("us".equalsIgnoreCase(type)) {
            fileUrl = vocDO.getUsPronounciation();
        } else if ("uk".equalsIgnoreCase(type)) {
            fileUrl = vocDO.getUkPronounciation();
        } else {
            throw new BusinessException(ResultCodeEnum.PARAM_ERROR);
        }

        if (fileUrl == null || fileUrl.isEmpty()) {
            throw new BusinessException(ResultCodeEnum.PARAM_ERROR);
        }

        // 3. 从URL中提取文件名
        String fileName = (fileUrl.contains("/")) ? fileUrl.substring(fileUrl.lastIndexOf("/") + 1) : fileUrl;

        // 4. 删除OSS上的文件
        try {
            fileUploadService.deleteFile(fileName);
        } catch (Exception e) {
            // 记录日志但不抛出异常，因为可能文件已经不存在
            log.warn("删除OSS文件失败: {}", fileName, e);
        }

        // 5. 更新数据库中的音频字段
        LambdaUpdateWrapper<VocDO> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(VocDO::getId, id);
        if ("us".equalsIgnoreCase(type)) {
            updateWrapper.set(VocDO::getUsPronounciation, null);
        } else if ("uk".equalsIgnoreCase(type)) {
            updateWrapper.set(VocDO::getUkPronounciation, null);
        }
        vocMapper.update(null, updateWrapper);

        // 6. 构造返回结果
        DeleteVocFileOperateDTO dto = new DeleteVocFileOperateDTO();
        dto.setVocId(id);
        dto.setContent(vocDO.getContent());
        dto.setType(type);
        dto.setFileName(fileName);

        return dto;
    }

    /**
     * 删除单词与章节、书籍的关系
     * @param id
     * @return
     */
    @Override
    @Transactional
    @OperateLog(dataType = "单词", operateType = 2, description = "删除单词与章节关系", logReturnValue = true)
    public DeleteVocRelationOperateDTO deleteVocRelation(Long id) {
        VocChapBookDO relation = vocChapBookMapper.selectById(id);
        if (relation == null)
            return null;
        vocChapBookMapper.deleteById(id);
        if (relation.getChapterId() != null) {
            vocChapterMapper.update(null, new LambdaUpdateWrapper<VocChapterDO>()
                    .eq(VocChapterDO::getId, relation.getChapterId())
                    .setSql("voc_count = voc_count - 1"));
        }
        vocBookMapper.update(null, new LambdaUpdateWrapper<VocBookDO>()
                .eq(VocBookDO::getId, relation.getBookId())
                .setSql("voc_count = voc_count - 1"));
        DeleteVocRelationOperateDTO dto = new DeleteVocRelationOperateDTO();
        dto.setId(id);
        return dto;
    }

    /**
     * 删除释义
     * @param id
     * @return
     */
    @Override
    @Transactional
    @OperateLog(dataType = "单词", operateType = 2, description = "删除单词释义", logReturnValue = true)
    public DeleteInterpretationOperateDTO deleteInterpretation(Long id) {
        VocMeaningDO meaning = vocMeaningMapper.selectById(id);
        if (meaning == null)
            return null;
        vocMeaningMapper.deleteById(id);
        DeleteInterpretationOperateDTO dto = new DeleteInterpretationOperateDTO();
        dto.setId(id);
        return dto;
    }

    /**
     * 删除例句
     * @param id
     * @return
     */
    @Override
    @Transactional
    @OperateLog(dataType = "单词", operateType = 2, description = "删除单词例句", logReturnValue = true)
    public DeleteVocExampleOperateDTO deleteExample(Long id) {
        ExampleSentenceDO example = exampleSentenceMapper.selectById(id);
        if (example == null)
            return null;
        exampleSentenceMapper.deleteById(id);
        DeleteVocExampleOperateDTO dto = new DeleteVocExampleOperateDTO();
        dto.setId(id);
        return dto;
    }

    // 根据id查询单词详细信息
    @Override
    public VocDTO getVocById(Long vocId) {
        VocDO vocDO = vocMapper.selectById(vocId);
        if (vocDO == null) {
            return null;
        }
        // 查询释义
        List<VocMeaningDO> meanings = vocMeaningMapper.selectList(
                new LambdaQueryWrapper<VocMeaningDO>().eq(VocMeaningDO::getVocId, vocId));
        vocDO.setInterpretations(meanings);
        // 查询例句
        List<ExampleSentenceDO> examples = exampleSentenceMapper.selectList(
                new LambdaQueryWrapper<ExampleSentenceDO>().eq(ExampleSentenceDO::getVocId, vocId));
        vocDO.setExamples(examples);
        // 转换为DTO
        return vocConverter.vocDO2DTO(vocDO);
    }
}
